class PinController < BaseController
  
  before_filter :login_required, :only => :create
  before_filter :find_user, :only => :create
  
  
  #【参数】
  #【返回值】
  #【注意】
  #【作者】 by ljl 2012.08.18
  def item
    if params[:id].blank?
      raise ActionController::UnknownAction
    else
      @pin = Pin.find_by_id(params[:id])
      respond_to do |format|
        format.html {
          if @pin.blank?
            render :template => "/pin/error"
          else
            @board = @pin.board
            @comments = @pin.comments
            @user = User.find_by_id(session[:user])
          end
        }
        format.js
      end
    end
  end
  
  #【参数】
  #【返回值】
  #【注意】
  #【作者】 by ljl 2012.08.19
  def edit
    @pin = Pin.find_by_id(params[:id])
    if @pin.blank?
      render :template => "/pin/error"
    else
      @user = @pin.board.user
      return unless request.post?
      begin
        @pin.attributes = params[:pin]
        @pin.save!
        redirect_to :controller => :pin, :action => :item, :id => @pin.id
      rescue ActiveRecord::RecordInvalid
        render :action => :edit
      end
    end
  end
  
  #【参数】
  #【返回值】
  #【注意】
  #【作者】 by ljl 2012.08.21
  def create
    unless request.post?
      @boards = @user.boards# rescue []
      return
    end
    attr = {:title => params[:title],
            :url => params[:url],
            :media => params[:media],
            :description => params[:caption],
            :board_id => params[:board_id]
    }
    @pin = Pin.new(attr)
    @pin.save!
    render :template => "/pin/complete", :locals => {:pin => @pin, :board => @pin.board}
  end
  
  #【参数】
  #【返回值】
  #【注意】
  #【作者】 by ljl 2012.08.23
  def add_comment
    begin
      user = User.find_by_id(params[:user_id])
      comment = Comment.create(:content => params[:text], :pin_id => params[:pin_id], :user_id => user.id)
      r = {:status => "success", 
           :text => params[:text], 
           :id => comment.id,
           :user_id => params[:id],
           :name => user.name,
           :alt => user.name,
           :image => "/images/avatar/noAvatar.jpg"
      }
    rescue => e
      pp e.message
      r = {:message => "未知错误"}
    end
    render :json => r.to_json
  end
  
  #【参数】
  #【返回值】
  #【注意】
  #【作者】 by ljl 2012.08.23
  def del_comment
    unless comment = Comment.find_by_id(params[:id])
      return false
    else
      comment.destroy
      respond_to do |format|
        format.js {
          render :update do |page|
            page << "jQuery('#comment_' + #{params[:id]}).slideUp('500');"
          end
        }
      end
    end
  end
  
  #【参数】
  #【返回值】
  #【注意】
  #【作者】 by ljl 2012.08.24
  def delete
    pin = Pin.find_by_id(params[:id])
    board = pin.board
    pin.destroy
    redirect_to :controller => :board, :action => :item, :id => board.id
  end
  
  #【参数】
  #【返回值】
  #【注意】
  #【作者】 by ljl 2012.08.24
  def like
    render :update do |page|
      if logged_in?
        user_pin = UserPin.find_or_create_by_user_id_and_pin_id(session[:user], params[:id])
        user_pin.like = true
        user_pin.save!
        page << "jQuery(\"[data_id='#{params[:id]}']\").html('取消喜欢');"
        page << "jQuery(\"[data_id='#{params[:id]}']\").attr('onclick', \"new Ajax.Request('/pin/unlike/#{params[:id]}', {asynchronous:true, evalScripts:true}); return false;\");"
      else
        page.redirect_to :controller => :user, :action => :login
      end
    end
  end
  
  #【参数】
  #【返回值】
  #【注意】
  #【作者】 by ljl 2012.08.24
  def unlike
    render :update do |page|
      if logged_in?
        user_pin = UserPin.find_or_create_by_user_id_and_pin_id(session[:user], params[:id])
        user_pin.like = false
        user_pin.save!
        page << "jQuery(\"[data_id='#{params[:id]}']\").html('<em></em>喜欢');"
        page << "jQuery(\"[data_id='#{params[:id]}']\").attr('onclick', \"new Ajax.Request('/pin/like/#{params[:id]}', {asynchronous:true, evalScripts:true}); return false;\");"
      else
        page.redirect_to :controller => :user, :action => :login
      end
    end
  end  
  
  private
  
  def find_user
    @user = User.find_by_id(session[:user])
  end
  
end
